package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.GoodsDao;
import com.qf.xiaomi.dao.GoodsTypeDao;
import com.qf.xiaomi.dao.impl.GoodsDaoImpl;
import com.qf.xiaomi.dao.impl.GoodsTypeDaoImpl;
import com.qf.xiaomi.entity.PageBean;
import com.qf.xiaomi.pojo.Goods;
import com.qf.xiaomi.pojo.GoodsType;
import com.qf.xiaomi.service.GoodsService;

import java.util.List;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
public class GoodsServiceImpl implements GoodsService {

    GoodsDao dao = new GoodsDaoImpl();

    @Override
    public PageBean<Goods> findGoodsListByTypeIdPage(int typeId, int pageSize, int pageNum) {
        //封装PageBean
        PageBean<Goods> pageBean = new PageBean();
        //当前页
        pageBean.setPageNum(pageNum);
        //每页条数
        pageBean.setPageSize(pageSize);
        //起始页
        pageBean.setStartPage(1);

        long totalCount = dao.getTotalCount(typeId);
        //总条数
        pageBean.setTotalCount(totalCount);


        //设置列表数据  limit m,n;
        int startIndex = pageSize * (pageNum - 1);
        List<Goods> data = dao.getData(typeId, startIndex, pageSize );
        pageBean.setData(data);

        return pageBean;
    }

    /**
     * 一对一查询
     * @param id
     * @return
     */
    @Override
    public Goods findById(int id) {
        //查询的商品详情信息
        Goods goods = dao.findById(id);

        //获取商品分类id
        int typeid = goods.getTypeid();
        GoodsTypeDao typeDao = new GoodsTypeDaoImpl();
        //查询商品分类信息
        GoodsType goodsType = typeDao.findById(typeid);

        goods.setGoodsType(goodsType);

        return goods;
    }
}
